package com.zcp.todayheadline;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

@ContentView(R.layout.activity_main)
public class MainActivity extends AppCompatActivity {

    @ViewInject(R.id.vp)
    ViewPager vp;

    @ViewInject(R.id.l1)
    LinearLayout l1;

    int[] images;
    ImageView[] ivs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        //初始化控件
        initView();
    }

    //初始化控件
    public void initView() {

        //图片数组
        images = new int[]{R.drawable.ff, R.drawable.gg, R.drawable.aa};

        //小圆点数组
        ivs = new ImageView[images.length];


        //设置适配器
        vp.setAdapter(new MyPager());

        //初始化小圆点
        initPoint();

        //监听
        vp.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                setbg(position);
                if (position == images.length - 1) {
                    //跳转
                    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                    startActivity(intent);

                    //关闭当前页面
                    finish();
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    //初始化小圆点
    public void initPoint() {
        for (int i = 0; i < images.length; i++) {
            ImageView image = new ImageView(MainActivity.this);
            //设置宽和高
            image.setLayoutParams(new ViewGroup.LayoutParams(30, 30));
            //设置缩放
            image.setScaleType(ImageView.ScaleType.FIT_XY);

            if (i == 0) {
                image.setImageResource(R.drawable.select);
            } else {
                image.setImageResource(R.drawable.normal);
            }
            ivs[i] = image;
            l1.addView(image);
        }

    }

    //设置适配器
    class MyPager extends PagerAdapter {
        @Override
        public int getCount() {
            return images.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            //创建图片对象
            ImageView image = new ImageView(MainActivity.this);
            //设置宽和高
            image.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
            //设置缩放
            image.setScaleType(ImageView.ScaleType.FIT_XY);

            image.setImageResource(images[position]);

            container.addView(image);

            return image;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((View) object);
        }
    }

    //设置圆点背景
    public void setbg(int index) {
        for (int i = 0; i < images.length; i++) {
            if (i == index) {
                ivs[i].setImageResource(R.drawable.select);
            } else {
                ivs[i].setImageResource(R.drawable.normal);
            }
        }
    }
}
